package com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.ChargeItemBatchAddReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.ChargeItemBatchExportReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.SmsWnsChargeOrgItemReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.SmsWnsIdListReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.service.SmsWnsChargeOrgItemService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 * 各专业收费项表 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2024-01-15
 */
@RestController
@SecretBody
@RequestMapping("/welcomeNewStudent/chargeItem")
public class SmsWnsChargeOrgItemController {
    @Resource
    private SmsWnsChargeOrgItemService smsWnsChargeOrgItemService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @ApiOperation("添加专业收费项")
    @PostMapping("/add")
    public ResponseResult addCategoryItem(@RequestBody SmsWnsChargeOrgItemReq categoryItemReq) {
        return smsWnsChargeOrgItemService.addCategoryItem(categoryItemReq);
    }

    @ApiOperation("专业收费项导出")
    @PostMapping("/batch/export")
    public ResponseResult batchExportChargeItem(@RequestBody ChargeItemBatchExportReq chargeItemBatchExportReq, HttpServletResponse response) {
        ResponseResult commonCodeEnum;
        try {
            Future<ResponseResult> future = smsWnsChargeOrgItemService.batchExportChargeItem(chargeItemBatchExportReq, response);
            while (true) {
                if (future.isDone()) {
                    commonCodeEnum = future.get();
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return commonCodeEnum;
    }

    @ApiOperation("专业收费项导入")
    @PostMapping("/batch/add")
    public ResponseResult batchAddChargeItem(@RequestBody ChargeItemBatchAddReq chargeItemBatchAddReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        ResponseResult commonCodeEnum;
        try {
            Future<ResponseResult> future = smsWnsChargeOrgItemService.batchAddChargeItem(chargeItemBatchAddReq, userId);
            while (true) {
                if (future.isDone()) {
                    commonCodeEnum = future.get();
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return commonCodeEnum;
    }

    @ApiOperation("删除专业收费项")
    @PostMapping("/del")
    public ResponseResult delCategoryItem(@RequestBody SmsWnsIdListReq idListReq) {
        if (idListReq.getIdList().isEmpty()) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        smsWnsChargeOrgItemService.removeByIds(idListReq.getIdList());
        return CommonResult.success();
    }

    @ApiOperation("修改专业收费项")
    @PostMapping("/update")
    public ResponseResult updateCategoryItem(@RequestBody SmsWnsChargeOrgItemReq categoryItemReq) {
        return smsWnsChargeOrgItemService.updateCategoryItem(categoryItemReq);
    }

    @ApiOperation("专业收费项列表")
    @GetMapping("/list")
    public ResponseResult listCategoryItem(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                           @RequestParam("planId") String planId,
                                           @RequestParam(value = "searchStr", required = false) String searchStr) {
        return smsWnsChargeOrgItemService.listCategoryItem(pageNum, pageSize, planId, searchStr);
    }

    @ApiOperation("专业收费项详情")
    @GetMapping("/detail")
    public ResponseResult categoryItemDetail(@RequestParam("id") String id) {
        return smsWnsChargeOrgItemService.categoryItemDetail(id);
    }

    @ApiOperation("获取不同收费范围下每个学生的缴费记录[1.专业 2.班级 3.人员]")
    @GetMapping("/studentPaymentRecord")
    public ResponseResult getStudentPaymentRecordByRangeType(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                             @RequestParam(value = "rangType") Integer rangType,
                                                             @RequestParam(value = "planId") String planId,
                                                             @RequestParam(value = "collegeName", required = false) String collegeName,
                                                             @RequestParam(value = "majorName", required = false) String majorName,
                                                             @RequestParam(value = "className", required = false) String className,
                                                             @RequestParam(value = "paymentType", required = false) Integer paymentType) {
        return smsWnsChargeOrgItemService.getStudentPaymentRecordByRangeType(pageNum, pageSize, rangType, planId, collegeName, majorName, className, paymentType);
    }
}

